package br.cs.correspondente.servicos;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JFormattedTextField;
import javax.swing.JTextField;


public class Validador {

	public static boolean validaCadastroVendedor(String nome, String cnpj,
			String enpreendimento, String cpf, String tel, String tel2,
			String tel3, String tel4) {

		if(!isString(nome) || !isString(cnpj) || !isString(enpreendimento))
			return false;
		if(!isCpf(cpf))
			return false;
		if(!isTelContato(tel) || !isTel(tel2) || !isTel(tel3) || !isTel(tel4))
			return false;
		
		return true;
	}

	public static boolean isTelContato(String tel) {
		String telefone = tel.replace("(","").replace(")","").replace("-","").replace(" ","");
		if(telefone.length() != 10)
			return false;
		if(tel == null || !isString(telefone))
			return false;
		return true;
	}
	public static boolean isTel(String tel) {
		String telefone = tel.replace("(","").replace(")","").replace("-","").replace(" ","");
		if(telefone.equalsIgnoreCase(""))
			return true;
		if(telefone.length() != 10)
			return false;
		if(tel == null || !isString(telefone))
			return false;
		return true;
	}


	public static boolean isCpf(String cpf) {

		try {
			if(!isCPFValido(cpf.replace(".", "").replace("-","")))
				return false;
		} catch (Exception exc) {
			System.out.println("Vish");
			return false;
		}
		return true;
	}

	public static boolean isString(String string) {
		if(string == null || string.isEmpty())
			return false;
		return true;
	}
	public static boolean isCPFValido(String cpf){

		String cpfInval[] = {"00000000000","11111111111", "22222222222", "33333333333", "44444444444", "55555555555", 
				"66666666666",	"77777777777", "88888888888", "99999999999"}; 		
		int cont,aux2,soma=0;
		int []dig = new int [11];
		int [] digVeri = new int[2];

		if(cpf.length() != 11)
			return false;

		for(cont=0;cont<9;cont++){
			if(cpfInval[cont].equals(cpf))
				return false;
		} 


		for(cont=0;cont<cpf.length();cont++)
			dig[cont] = Character.getNumericValue(cpf.charAt(cont));

		// obtem o primeiro digito verificador
		aux2=10;
		for(cont=0;cont<9;cont++,aux2--){
			soma+= dig[cont] * aux2;
		}
		digVeri[0]=soma % 11;
		digVeri[0]= digVeri[0] < 2 ? 0 : 11-digVeri[0];

		soma=0;
		aux2=11;

		for(cont=0;cont<10;cont++,aux2--){
			if(cont==9)
				soma+= digVeri[0] * aux2;
			else
				soma+= dig[cont] * aux2;
		}

		digVeri[1]=soma % 11;
		digVeri[1]= digVeri[1]<2 ? 0 : 11-digVeri[1];
		if(digVeri[0]!=dig[9] || digVeri[1]!=dig[10])
			return false;
		return true;
	}

	public static boolean isCEP(String cep) {
		Pattern pattern = Pattern.compile("^\\d{5,5}-?\\d{3,3}$");
		Matcher m = pattern.matcher(cep.replace("-",""));
		return m.matches();
	}

	public static boolean isValor(String campo) {
		Double valor = null;
		boolean flag = false;
		if(campo == null || campo.isEmpty())
			return false;
		try{
			valor = Double.parseDouble(campo);
			if(valor > 0)
				flag = true;
		}catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return flag;
	}

	public static boolean isImovel(JFormattedTextField txtCEPImovel,
			JTextField txtComplementoEndImovel, JTextField txtNumeroImovel,
			JTextField txtBairroImovel, JTextField txtEnderecoImovel,
			JTextField txtValorImovel, JTextField txtValorAvaliadoImovel) {
		
		if(!isCEP(txtCEPImovel.getText()) || 
				!isString(txtComplementoEndImovel.getText()) || 
				!isString(txtNumeroImovel.getText()) || 
				!isString(txtBairroImovel.getText()) || 
				!isString(txtEnderecoImovel.getText()) ||
				!isString(txtValorImovel.getText()) ||
				!isString(txtValorAvaliadoImovel.getText())){
			return false;
				}
		
		return true;
	}
}
